package com.lzxuni.modules.qualityAssurance;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.common.annotation.SysLog;
import com.lzxuni.common.utils.FileUtil;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.date.DateUtil;
import com.lzxuni.common.utils.poi.ExcelUtil;
import com.lzxuni.config.AttachmentConfig;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.Tree;
import com.lzxuni.modules.common.service.FileEntityService;
import com.lzxuni.modules.dataAcquisition.entity.Chain;
import com.lzxuni.modules.dataAcquisition.entity.ProductIndex;
import com.lzxuni.modules.dataAcquisition.service.ProductIndexService;
import com.lzxuni.modules.qualityAssurance.entity.ProductSampling;
import com.lzxuni.modules.qualityAssurance.entity.ProductSamplingIndex;
import com.lzxuni.modules.qualityAssurance.service.ProductSamplingIndexService;
import com.lzxuni.modules.qualityAssurance.service.ProductSamplingService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.SQLException;
import java.util.*;

@RestController
@RequestMapping("/LR_QualityAssurance/AuditData")
public class AuditDataController extends BaseController {

    @Autowired
    private ProductSamplingService samplingService;
    @Autowired
    private ProductSamplingIndexService indexService;
    @Autowired
    private ProductIndexService productIndexService;
    @Autowired
    private FileEntityService fileEntityService;
    @Autowired
    private AttachmentConfig attachmentConfig;

    @RequestMapping("/Index")
    @SysLog(categoryId = 2, module = "抽检查看", operateType = "访问")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("/LR_QualityAssurance/AuditData/index");
        return mv;
    }

    @RequestMapping("/GetList")
    public Object GetList(String pagination, String traceabilityCode, String productName) throws SQLException {
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        Map map = new HashMap();
        map.put("traceabilityCode", traceabilityCode);
        map.put("productName", productName);
        PageData pageData = getPageData(samplingService.queryPage(pageParameter, map));
        return R.ok().put("data", pageData);
    }

    @RequestMapping("/Form")
    public ModelAndView insert(String keyValue, String type) throws Exception {
        ModelAndView mv = new ModelAndView("/LR_QualityAssurance/AuditData/form");
        ProductSampling sampling = samplingService.queryOne(keyValue);
        List<ProductSamplingIndex> indexList = indexService.list(new QueryWrapper<ProductSamplingIndex>().eq("sampling_id", keyValue));
        if (indexList != null && indexList.size() > 0) {
            for (ProductSamplingIndex index : indexList) {
                ProductIndex productIndex = productIndexService.getById(index.getProductIndexId());
                if (productIndex != null) {
                    if (productIndex.getCollectionType().equals("图片") || productIndex.getCollectionType().equals("视频")) {
                        FileEntity fileEntity = new FileEntity();
                        fileEntity.setYwId(productIndex.getProductIndexId());
                        fileEntity.setYwType(productIndex.getCollectionType().equals("图片") ? "indexPic" : "indexFile");
                        productIndex.setFileEntityList(fileEntityService.queryListByFileEntity(fileEntity));
                    }
                }
                index.setProductIndex(productIndex);
            }
        }
        sampling.setProductSamplingIndexList(indexList);
        FileEntity fileEntity = new FileEntity();
        fileEntity.setYwId(keyValue);
        fileEntity.setYwType("proofFiles");
        sampling.setFileEntityList(fileEntityService.queryListByFileEntity(fileEntity));
        mv.addObject("sampling", sampling);
        mv.addObject("type", type);
        return mv;
    }


    @RequestMapping("/DeleteForm")
    @SysLog(categoryId = 3, module = "抽检查看", operateType = "删除")
    public Object delete(String keyValue) {
        /*indexService.remove(new QueryWrapper<ProductSamplingIndex>().eq("sampling_id", keyValue));
        samplingService.removeById(keyValue);*/
        return R.ok("删除成功");
    }

    @RequestMapping("/exportForm")
    public void exportForm(HttpServletResponse response, String src, HttpServletRequest request) throws Exception{
        String basePath = attachmentConfig.getPath();
        File file = new File(basePath + src);
        InputStream fis = new FileInputStream(file);
        response.setContentType("multipart/form-data");
        String nameString = "举证材料" + DateUtil.DateToString(new Date(), "yyyyMMddHHmmss");
        String fileName = file.getName();
        String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1);
        String formFileName = new String(nameString.getBytes("UTF-8"), "ISO-8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + formFileName + "." +suffixName);
        try {
            OutputStream os = response.getOutputStream();
            byte[] b = new byte[1024 * 1024 * 200];
            int len;
            while ((len = fis.read(b)) > 0) {
                os.write(b, 0, len);
            }
            os.flush();
            os.close();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
